package com.fcx.workai.loginmodule.ui.login;


import com.hxh.component.basicore.component.mvp.persenter.BasePresenter;
import com.hxh.component.basicore.component.net.IApiError;
import com.hxh.component.basicore.util.Utils;
import com.hxh.component.basicore.util.rx.normalhttpstyle.NormalSubscribe;
import com.hxh.component.basicore.util.rx.resetfulhttpstyle.ProgressSubScribe;
import com.hxh.component.business.app.App;
import com.hxh.component.business.bean.OpenProgressBean;
import com.hxh.component.business.common.UserInfoBean;
import com.hxh.component.business.ui.bean.UserCompanyListBean;

import com.fcx.workai.loginmodule.api.ApiFactory;
import com.fcx.workai.loginmodule.api.request.Request_PwdLogin;
import com.fcx.workai.loginmodule.api.request.Request_RefreshToken;
import com.hxh.component.business.utils.Constant;

import rx.Observable;
import rx.functions.Func1;

import static com.hxh.component.business.utils.Constant.TENANT_ID;

/**
 * Created by hxh on 2017/8/4.
 */

/**
 * 创建者：hxh
 * 时间：  2017/8/4
 * 描述：
 */
public class PwdLoginPresenter extends BasePresenter<PwdLoginFragment> implements PwdLoginContract.P {

    @Override
    public void login(String acount, String pwd, String scope) {

        addSubscription(
                ApiFactory
                        .login_pwd(new Request_PwdLogin(acount, pwd, scope))
                        .subscribe(new ProgressSubScribe<UserInfoBean>(true) {
                            @Override
                            public void _OnError(Throwable msg) {
                                IApiError apiError = getApiError(msg);
                                if(apiError!=null){
                                    if (apiError.getErrorCode() == 404) {
                                        Utils.Toast.toast("该账号没有加入蜗壳，请重试");
                                    } else
                                    {
                                        Utils.Toast.toast(apiError.getErrorMessage());
                                    }
                                }else{
                                    Utils.Toast.toast("登录异常，请重试");
                                }
                            }

                            @Override
                            public void _OnNet(UserInfoBean userInfoBean) {
                                App.getUserDTO().setUser(userInfoBean);
                                getView().showLoginResule(userInfoBean);
                            }
                        }));
    }
}
